package com.umakr.demo.controller;

import com.sun.org.apache.regexp.internal.RE;
import com.umakr.demo.model.Kecheng;
import com.umakr.demo.model.Teacher;
import com.umakr.demo.model.TeacherKecheng;

import com.umakr.demo.service.KechengService;
import com.umakr.demo.service.TeacherKechengSerivice;
import com.umakr.demo.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yangliu on 2017/8/19.
 */
@Controller
@RequestMapping("/TeacherKechengController")
public class TeacherKechengController {

    @Autowired
    TeacherKechengSerivice teacherKechengSerivice;

    @Autowired
    TeacherService teacherService;

    @Autowired
    KechengService kechengService;

    @RequestMapping("/showTeacherKecheng")
    public String showTeacherKecheng(){
        return "StudentManage/teacherkecheng";
    }

    @RequestMapping("/teacherData")
    @ResponseBody
    public List teacherData(){
        List<Teacher> list = teacherService.showAll();
        return list;
    }

    @RequestMapping("/kechengDate")
    @ResponseBody
    public List kechengDate(){
        List<Kecheng> list =kechengService.showAll();
        return list;
    }

    @RequestMapping("/teacherShowKecheng")
    @ResponseBody
    public List teacherShowKecheng(Long tid) {
        List<Kecheng> list = teacherKechengSerivice.teacherToKecehng(tid);
//        List<TeacherShowKechengMapper> list1 = new ArrayList<>();
//        for (int i =0;i<list.size();i++){
//            TeacherShowKechengMapper teacherShowKecheng = new TeacherShowKechengMapper(list.get(i).getKid().toString(),list.get(i).getKname());
//            list1.add(teacherShowKecheng);
//        }
//        return list1;
        return list;
    }



    @RequestMapping("/allSelectedKecheng")
    @ResponseBody
    public void allSelectedKecheng(@RequestParam(value = "str[]",required = false) String[] str,Long tid){
//        全部删除
        teacherKechengSerivice.deleteAll(tid);
        for (int i =0;i<str.length;i++){
            System.out.println(Long.parseLong(str[i]));
            System.out.println(tid);
            TeacherKecheng teacherKecheng = new TeacherKecheng(tid,(Long.parseLong(str[i])));
            //执行添加
            teacherKechengSerivice.addAll(teacherKecheng);
        }
    }
}
